package com.analyse.stock.strategy.indicator;


import org.ta4j.core.Bar;
import org.ta4j.core.BarSeries;
import org.ta4j.core.Indicator;
import org.ta4j.core.num.Num;

/**
 * 长上影判断
 */
public class LongShadowIndicator implements Indicator<Boolean> {

    private final BarSeries series;
    private final double shadowRatioThreshold; // 长影线占比阈值

    public LongShadowIndicator(BarSeries series, double shadowRatioThreshold) {
        this.series = series;
        this.shadowRatioThreshold = shadowRatioThreshold;
    }

    @Override
    public Boolean getValue(int index) {
        if (index < 0 || index >= series.getBarCount()) {
            return false; // 边界情况
        }
        Bar bar = series.getBar(index);
        Num open = bar.getOpenPrice();
        Num close = bar.getClosePrice();
        Num high = bar.getHighPrice();
        Num low = bar.getLowPrice();

        // 计算实体高度和影线长度
        Num bodyHeight = open.minus(close).abs();
        Num upperShadow = high.minus(open.max(close)); // 上影线
        Num lowerShadow = open.min(close).minus(low);  // 下影线

        // 计算总高度
        Num totalHeight = high.minus(low);

        // 判断影线占比
        boolean hasLongUpperShadow = upperShadow.isGreaterThan(totalHeight.multipliedBy(series.numOf(shadowRatioThreshold)));
        boolean hasLongLowerShadow = lowerShadow.isGreaterThan(totalHeight.multipliedBy(series.numOf(shadowRatioThreshold)));

        // 根据业务需求，只判断长下影线
        //return hasLongLowerShadow && !hasLongUpperShadow;
        return hasLongUpperShadow;
    }

    @Override
    public BarSeries getBarSeries() {
        return series;
    }

    @Override
    public Num numOf(Number number) {
        return series.numOf(number);
    }
}